package m.system.db;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.Map;

public class DataRow {
	private final Map<String,Object> map;
	protected DataRow(Map<String,Object> map){
		this.map=map;
	}
	/**
	 * 获取值 并转换
	 *  clazz 值的类型
	 *  name field
	 *
	 */
	@SuppressWarnings("unchecked")
	public <T> T get(Class<T> clazz, String name){
		Object value=get(name);
		T result=null;
		if(null==value) return null;
		else if(clazz.isInstance(value)) result= (T) map.get(name);
		else if(value instanceof Boolean){
			Integer v=(Boolean)value?1:0;
			if(clazz.equals(Integer.class)) result= (T) v;
			else if(clazz.equals(Double.class)) result= (T) new Double((v).doubleValue());
			else if(clazz.equals(Float.class)) result= (T) new Float((v).floatValue());
			else if(clazz.equals(Short.class)) result= (T) new Short((v).shortValue());
			else result= (T) String.valueOf(v);
		}else if(value instanceof BigDecimal) {
			//数字类型
			if(clazz.equals(Integer.class)) result= (T) new Integer(((BigDecimal)value).intValue());
			else if(clazz.equals(Double.class)) result= (T) new Double(((BigDecimal)value).doubleValue());
			else if(clazz.equals(Float.class)) result= (T) new Float(((BigDecimal)value).floatValue());
			else if(clazz.equals(Long.class)) result= (T) new Long(((BigDecimal)value).longValue());
			else if(clazz.equals(Short.class)) result= (T) new Short(((BigDecimal)value).shortValue());
			else result= (T) ((BigDecimal)value).toPlainString();
		}else if(value instanceof Long) {
			//long
			if(clazz.equals(Integer.class)) result= (T) new Integer(((Long)value).intValue());
			else if(clazz.equals(Double.class)) result= (T) new Double(((Long)value).doubleValue());
			else if(clazz.equals(Float.class)) result= (T) new Float(((Long)value).floatValue());
			else if(clazz.equals(Short.class)) result= (T) new Short(((Long)value).shortValue());
				//long >> date
			else if(clazz.equals(Date.class)) result= (T) new Date((Long)value);
			else result= (T) String.valueOf(value);
		}else if(value instanceof Double) {
			//double
			if(clazz.equals(Integer.class)) result= (T) new Integer(((Double)value).intValue());
			else if(clazz.equals(Float.class)) result= (T) new Float(((Double)value).floatValue());
			else if(clazz.equals(Long.class)) result= (T) new Long(((Double)value).longValue());
			else if(clazz.equals(Short.class)) result= (T) new Short(((Double)value).shortValue());
			else result= (T) String.valueOf(value);
		}else if(value instanceof Float) {
			//float
			if(clazz.equals(Integer.class)) result= (T) new Integer(((Float)value).intValue());
			else if(clazz.equals(Double.class)) result= (T) new Double(((Float)value).doubleValue());
			else if(clazz.equals(Float.class)) result= (T) value;
			else if(clazz.equals(Short.class)) result= (T) new Short(((Float)value).shortValue());
			else result= (T) String.valueOf(value);
		}else if(value instanceof Short) {
			//short
			if(clazz.equals(Integer.class)) result= (T) new Integer(((Short)value).intValue());
			else if(clazz.equals(Double.class)) result= (T) new Double(((Short)value).doubleValue());
			else if(clazz.equals(Float.class)) result= (T) new Float(((Short)value).floatValue());
			else if(clazz.equals(Long.class)) result= (T) new Long(((Short)value).longValue());
				//short >> date
			else if(clazz.equals(Date.class)) result= (T) new Date(((Short) value).longValue());
			else result= (T) String.valueOf(value);
		}else if(value instanceof Integer) {
			//int
			if(clazz.equals(Double.class)) result= (T) new Double(((Integer)value).doubleValue());
			else if(clazz.equals(Float.class)) result= (T) new Float(((Integer)value).floatValue());
			else if(clazz.equals(Long.class)) result= (T) new Long(((Integer)value).longValue());
			else if(clazz.equals(Short.class)) result= (T) new Short(((Integer)value).shortValue());
				//int >> date
			else if(clazz.equals(Date.class)) result= (T) new Date(((Integer) value).longValue());
			else result= (T) String.valueOf(value);
		}else if(value instanceof LocalDateTime){
			if(clazz.equals(Date.class)) result= (T) Date.from(((LocalDateTime)value).toInstant(ZoneOffset.of("+8")));
		}
		if(null==result) result=(T) map.get(name);
		return result;
	}
	/**
	 * 获取值 返回Object,需自己转换
	 *  name
	 *
	 */
	public Object get(String name){
		return map.get(name);
	}
}
